package com.jeff.android.lab.jetpack.viewpager2

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.view.get
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import butterknife.BindView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.jeff.android.lab.BaseFragment
import com.jeff.android.lab.R
import com.jeff.android.lab.jframework.RuntimeManager
import com.jeff.android.lab.util.CircleTransform
import com.jeff.android.lab.util.RoundCornerTransform
import com.jeff.utils.EasyBlur
import com.jeff.utils.ImageUtil
import com.jeff.utils.ToastUtil

/**
 * @author: dingfeng
 * @date: 2020-12-01
 * @since: 1.0.0
 **/
class ViewPager2TransformFragment : BaseFragment() {

    val myData: List<String> = mutableListOf(
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607320238205&di=c01ff2e86c62e8461039828fbda8e82a&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607320238205&di=80b7cb774167d31b56d7c85940996280&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F27%2F67%2F01300000921826141299672233506.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607320238203&di=5cbc59a0e0cdcc181a80593238900058&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F25%2F99%2F19300000421423134190997943822.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607320238202&di=cfafbe4cc5b92b466368e8acc170970e&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F02%2F38%2F01300000237560123245382609951.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607320238201&di=0e9fe1a77110de0164b90f6c0e2e6cf6&imgtype=0&src=http%3A%2F%2Fi1.sinaimg.cn%2FIT%2F2010%2F0419%2F201041993511.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607320238199&di=584509901d5c76669851c1ab1349e1af&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F20%2F39%2F01300542519189139990390839214.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607355277815&di=f1f59fbcfd5b38e4513e69f6b6097b2b&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F63%2F70%2F06300000046969120433706514748.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607355277814&di=2838f9c6a0aef58a8991c6f20cd0a70e&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F12%2F87%2F01300001149956130041875096065.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607355356938&di=170ce34826cd5ef41ef5fe7e7b03d0b4&imgtype=0&src=http%3A%2F%2Fa1.att.hudong.com%2F81%2F71%2F01300000164151121808718718556.jpg"
    )

    @BindView(R.id.view_pager)
    lateinit var mViewPager: ViewPager2

    @BindView(R.id.iv_left)
    lateinit var iv_left: ImageView

    @BindView(R.id.iv_right)
    lateinit var iv_right: ImageView

    var mCurrentPageIndex: Int = 0

    override fun getRootLayout(): Int {
        return R.layout.jetpack_viewpager2_transtorm_fragment
    }

    override fun initView() {
        mViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                ToastUtil.show(mContext, "onPageSelected:$position")

                mCurrentPageIndex = position
            }
        })

        setTransform()


        val blurBitmap = EasyBlur.with(RuntimeManager.instance().appContext)
                .policy(EasyBlur.BlurPolicy.FAST_BLUR)
                .bitmap(ImageUtil.drawable2Bitmap(resources.getDrawable(R.drawable.bg_blur)))
                .radius(5)
                .blur()
        iv_left.setImageBitmap(blurBitmap)
        iv_right.setImageBitmap(blurBitmap)
    }

    override fun initData() {
        val adapter = MyAdapter(myData)
        adapter.setData(myData)
        mViewPager.adapter = adapter

    }

    fun setTransform() {
        mViewPager.offscreenPageLimit = 1

        val recyclerView = mViewPager.getChildAt(0) as RecyclerView

//        val padding = resources.getDimensionPixelOffset(R.dimen.dp_40) + resources.getDimensionPixelOffset(R.dimen.dp_40)

        val padding = resources.getDimensionPixelOffset(R.dimen.dp_10) * 6

        // setting padding on inner RecyclerView puts overscroll effect in the right place
        recyclerView.setPadding(padding, 0, padding, 0)
        recyclerView.clipToPadding = false
        val compositePageTransformer = CompositePageTransformer()
        compositePageTransformer.addTransformer(ScaleInTransformer())
//        compositePageTransformer.addTransformer(MarginPageTransformer(resources.getDimension(R.dimen.dp_10).toInt()))
        compositePageTransformer.addTransformer(MarginPageTransformer(resources.getDimension(R.dimen.dp_10).toInt() * 3))
        mViewPager.setPageTransformer(compositePageTransformer)
    }


    class MyAdapter(var d: List<String>) : RecyclerView.Adapter<PagerViewHolder>() {

        private lateinit var mData: List<String>

        fun setData(data: List<String>) {
            mData = data
        }


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagerViewHolder {
            val itemView = LayoutInflater.from(parent.context).inflate(R.layout.jetpack_viewpager2_transform_item, parent, false)
            return PagerViewHolder(itemView)
        }

        override fun getItemCount(): Int {
            return mData.size
        }

        override fun onBindViewHolder(holder: PagerViewHolder, position: Int) {
            holder.bindData(mData[position])
        }

    }

    class PagerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val imageView: ImageView = itemView.findViewById(R.id.image)

        fun bindData(url: String) {

            Glide.with(imageView).load(url)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .transform(CenterCrop(), RoundCornerTransform(30))
                    .into(imageView)

//            Glide.with(imageView).asBitmap().load(url)
//                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
//                    .transform(CenterCrop())
//                    .into(object : CustomTarget<Bitmap>() {
//                        override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
//                            imageView.setImageBitmap(resource)
//
//                            val position = adapterPosition
//
//                            // 高斯模糊
//                            val blurBitmap = EasyBlur.with(RuntimeManager.instance().appContext)
//                                    .policy(EasyBlur.BlurPolicy.FAST_BLUR)
//                                    .bitmap(resource)
//                                    .radius(5)
//                                    .blur()
//                            imageView.setImageBitmap(blurBitmap)
//
//                        }
//
//                        override fun onLoadCleared(placeholder: Drawable?) {
//                        }
//                    })
        }
    }

}